package com.gzdh.dishrecognition.ui.view;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;

public class SlidePopupWindow extends PopupWindow {

    boolean left;

    public SlidePopupWindow(Context context, int resource, int width, int height) {
        this(context, resource, width, height, false);
    }

    public SlidePopupWindow(Context context, int resource, int width, int height, boolean left) {
        this.left = left;
        //设置view
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(resource, null);
        setContentView(view);
        initView();
        //其他设置
        setWidth(dp2px(width));//必须设置宽度
        setHeight(dp2px(height));//必须设置高度
        setFocusable(false);//是否获取焦点
        setOutsideTouchable(true);//是否可以通过点击屏幕外来关闭
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                dismiss();
                return false;
            }
        });
    }

    @Override
    public void dismiss() {
        // TODO Auto-generated method stub
        ObjectAnimator translationX = ObjectAnimator.ofFloat(getContentView(), "translationX", 0, (left ? -1 : 1) * getWidth());
        translationX.setDuration(500).addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                try {
                    SlidePopupWindow.super.dismiss();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        translationX.start();
    }

    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor);
        //加入动画
        ObjectAnimator.ofFloat(getContentView(), "translationX",  (left ? -1 : 1) * getWidth(), 0).setDuration(500).start();
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        //加入动画
        ObjectAnimator.ofFloat(getContentView(), "translationX",  (left ? -1 : 1) * getWidth(), 0).setDuration(500).start();
    }

    /**
     * 获取屏幕宽高
     *
     * @param context
     * @return
     */
    private static int[] getScreenSize(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return new int[]{displayMetrics.widthPixels, displayMetrics.heightPixels};
    }

    /**
     * Value of dp to value of px.
     *
     * @param dpValue The value of dp.
     * @return value of px
     */
    public static int dp2px(final float dpValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int px2dp(Context context,float px){
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px/scale+0.5f);
    }

    public static int getDensityDpi() {
        return Resources.getSystem().getDisplayMetrics().densityDpi;
    }

    private void initView() {

    }
}
